package net.lwf.web.struts.action.maths.wp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.handler.IProcessHandler;
import lwf.web.struts.action.constant.IActionConstant;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.constant.IHandlerConstant;
import net.lwf.handler.wp.WordProblemHandler;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IPageTitle;
import net.lwf.web.constant.IWebConstant;
import net.lwf.web.struts.action.LwfWorksheetAction;
import net.lwf.web.struts.action.LwfWorksheetActionForm;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class WordProblemAction extends LwfWorksheetAction {
	
	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return generate(mapping, form, request, response);
	} //home
	
	public ActionForward generate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		
		LwfWorksheetActionForm baseform = (LwfWorksheetActionForm) form;
		WordProblemHandler handler = (WordProblemHandler) getHandler(request);
		QuestionInfo qinfo = handler.generateSingle();
		//Set the question data
		setQuestionData(request, qinfo);
		//Set radio answers
		WebUtil.setAttribute(request, IWebConstant.RADIO_ANSWERS, qinfo.getRadioAnswerChoices());
		//Set form data
		baseform.setQuestion(qinfo.getQuestion());
		baseform.setQuestionNumber(getQuestionNumber(request));
		return mapping.findForward("display");
	} //generate
	
	public IProcessHandler getHandler(HttpServletRequest request) {
		if(getOperation().equalsIgnoreCase(IHandlerConstant.TIME_WORD_PROBLEM)) {
			return new WordProblemHandler(getGrade(), IHandlerConstant.WORD_PROBLEM_TYPE_TIME);
		}
		if(getOperation().equalsIgnoreCase(IHandlerConstant.ARITHMATICS_WORD_PROBLEM)) {
			return new WordProblemHandler(getGrade(), IHandlerConstant.WORD_PROBLEM_TYPE_ARITHMATICS);
		}
		return null;
	}

	@Override
	public String getPageTitle() {
		if(getOperation().equalsIgnoreCase(IHandlerConstant.TIME_WORD_PROBLEM)) {
			return IPageTitle.TIME_WORD_PROBLEMS;
		}
		if(getOperation().equalsIgnoreCase(IHandlerConstant.ARITHMATICS_WORD_PROBLEM)) {
			return IPageTitle.ARITHMATICS_WORD_PROBLEMS;
		}
		return null;
	}

	@Override
	public String getMainTitle() {
		return IPageTitle.WORD_PROBLEMS;
	}

	@Override
	public String getPdfLink() {
		return IActionConstant.WORD_PROBLEM_PDF_LINK;
	}

	@Override
	public String getQuizLink() {
		//return IActionConstant.WORD_PROBLEM_QUIZ_LINK;
		return null;
	}

}
